import datetime
import os
import random

from PyQt5.QtSql import QSqlDatabase
from sqlalchemy import create_engine, Column, Integer, String, Float, ForeignKey, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from database import Base, engine, SessionLocal
from model.model import *

if __name__ == '__main__':
    db = SessionLocal()
    questions = db.query(Question).all()
    for question in questions:
        db.add(Solution(
            user_id=4,
            question_id=question.id,
            state=SolutionState.NOTSTART,
        ))
    db.commit()
    # try:
    #     total_tags = ['string', 'array', 'dp', 'tree', 'graph', 'union', 'diff', 'window', 'double', 'hash', 'stack', 'queue', 'dfs', 'segment', 'treenums', 'topo', 'sort', 'binary', 'bit', 'bits', 'tanxin', 'backtrack']
    #     for tag_name in total_tags:
    #         tag = db.query(Tag).filter_by(name=tag_name).first()
    #         print(tag_name)
    #         if not tag:
    #             tag = Tag(name=tag_name)
    #             db.add(tag)
    #     db.commit()
    #     #
    #     dir = './problems'
    #
    #     for file in os.listdir(dir):
    #         file_path = os.path.join(dir, file)
    #         if os.path.isfile(file_path):
    #             with open(file_path, 'r', encoding='utf-8') as f:
    #                 info = f.read()
    #
    #         title = info.split('\n\n')[0]
    #         question = db.query(Question).filter_by(title=title).first()
    #         if question:
    #             continue
    #         content = info.split('\n\n')[1]
    #         difficulty = random.choice(['easy', 'medium', 'hard'])
    #         tags = list(set(random.choices(
    #             total_tags,
    #             k=2  # 随机选择2个标签
    #         )))
    #         tpe = random.choice(['算法', '数据结构', '人工智能'])
    #
    #         db.add(Question(title=title, content=content, difficulty=difficulty, type=tpe, publisher_id=1))
    #         db.commit()
    #         question = db.query(Question).filter_by(title=title).first()
    #         for tag_name in tags:
    #             tag = db.query(Tag).filter_by(name=tag_name).first()
    #             if not tag:
    #                 tag = Tag(name=tag_name)
    #                 db.add(tag)
    #                 db.commit()
    #             db.add(QuestionTag(question_id=question.id, tag_id=tag.id))
    #         db.commit()
    #         solution = Solution(question_id=question.id, user_id=1, state=SolutionState.NOTSTART)
    #         db.add(solution)
    #         db.commit()
    # except Exception as e:
    #     print(e)
    #     db.rollback()
    # finally:
    #     db.close()